package com.hs.srm.admin.modules.pricemanagement.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.pricemanagement.dao.EssentialInformationDao;
import com.hs.srm.admin.modules.pricemanagement.entity.EssentialInformationEntity;
import com.hs.srm.admin.modules.pricemanagement.entity.InquiryQuotationEntity;
import com.hs.srm.admin.modules.pricemanagement.service.EssentialInformationService;
import com.hs.srm.admin.modules.pricemanagement.vo.EssentialInformationVO;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Service("essentialInformationServiceImpl")
public class EssentialInformationServiceImpl extends ServiceImpl<EssentialInformationDao, EssentialInformationEntity> implements EssentialInformationService {

    @Autowired
    private EssentialInformationDao essentialInformationDao;

    @Override
    public boolean insertEssntiaInfo(EssentialInformationEntity essentialInformationEntity) {
        boolean flg = this.insert(essentialInformationEntity);
        return flg;
    }

    @Override
    public EssentialInformationEntity findByNo(String vendorCode,
                                               String factory, String materialCode, String classify) {
        return this.baseMapper.findByNo(vendorCode, factory, materialCode,classify);
    }

    /**
     * 分页查询
     */
    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        // 从列表页面获取对应的值
        String materialCode = (String) params.get("materialCode");//采购申请单号
        String factory = (String) params.get("factory");//采购申请单号
        Page<EssentialInformationEntity> page = this.selectPage(
                new QueryParam<EssentialInformationEntity>(params).getPage(),
                new EntityWrapper<EssentialInformationEntity>()
                        .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .like(StringUtils.isNotBlank(factory), "werks", factory)
                        .addFilterIfNeed(!ObjectUtils.isEmpty(params.get(Constant.SQL_FILTER)),
                                (String) params.get(Constant.SQL_FILTER)));
        return new PageUtils(page);
    }


    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        // 从列表页面获取对应的值
        String materialCode = (String) params.get("materialCode");//采购申请单号
        String factory = (String) params.get("factory");//采购申请单号

        Page<EssentialInformationEntity> page = this.selectDetailPage(
                new QueryParam<EssentialInformationEntity>(params).getPage(),
                new EntityWrapper<EssentialInformationEntity>()
                        .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .like(StringUtils.isNotBlank(factory), "werks", factory),
                params);
        return new PageUtils(page);
    }

    private Page<EssentialInformationEntity> selectDetailPage(Page<EssentialInformationEntity> page,
                                                              Wrapper<EssentialInformationEntity> wrapper, Map<String, Object> params) {
        page.setRecords(this.essentialInformationDao.selectDetailPage(page, wrapper, params));
        return page;
    }

    @Override
    public List<EssentialInformationVO> findByNos(String factory, String materialCode, String pkId) {

        return this.baseMapper.findByNos(factory, materialCode, pkId);
    }

    @Override
    public void update(String vendorCode, String factory, String materialCode, String quotationId) {
        this.essentialInformationDao.update(vendorCode, factory, materialCode, quotationId);

    }

    @Override
    public boolean insertInquiryQuoToEssntiaInfo(
            InquiryQuotationEntity inquiryQuotationEntity) {
        boolean flag = false;
        if (inquiryQuotationEntity != null) {
            flag = this.essentialInformationDao.insertInquiryQuoToEssntiaInfo(inquiryQuotationEntity);
            return flag;
        }
        return flag;
    }

    public List<EssentialInformationVO> findByEssentialInforma(String factory, String materialCode) {

        return this.essentialInformationDao.findByEssentialInforma(factory, materialCode);
    }

    @Override
    public List<EssentialInformationVO> simplfindByNos(String factory,
                                                       String materialCode, String pkId) {

        return this.baseMapper.simplfindByNos(factory, materialCode, pkId);
    }

    //根据供应商编码和itemID查找数据
    @Override
    public EssentialInformationVO findByCodeItemId(String sgId, String itemId) {
        return this.baseMapper.findByCodeItemId(sgId, itemId);
    }

}
